﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Orders.Services;
using Autorent.Desktop.Modules.Orders.Views;

namespace Autorent.Desktop.Modules.Orders.ViewModels
{
    public class ApproveOrderViewModel : ViewModelBase
    {
        #region Public Properties

        public ApproveOrderView View { get; set; }

        #endregion

        #region Constructors

        public ApproveOrderViewModel(AutoOrder autoOrder, ApproveOrderView view, IUnityContainer container, IRegionManager regionManager,
            IEventAggregator eventAggregator, IAutoOrdersRepository repository, ISystemUsersRepository systemUsersRepository,
            IPaymentsRepository paymentsRepository)
        {
            this.View = view;
            view.Model = this;

            this._container = container;
            this._regionManager = regionManager;
            this._eventAggregator = eventAggregator;
            this._repository = repository;
            this._paymentsRepository = paymentsRepository;
            this._systemUsersRepository = systemUsersRepository;

            this.autoOrder = autoOrder;
        }

        #endregion

        #region Commands

        public ICommand ApproveOrder
        {
            get
            {
                return new RelayCommand(param => approveOrder());
            }
        }

        public ICommand Cancel
        {
            get
            {
                return new RelayCommand(param => cancel());
            }
        }

        #endregion

        #region Privat Variables

        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;
        private readonly IAutoOrdersRepository _repository;
        private readonly IPaymentsRepository _paymentsRepository;
        private readonly ISystemUsersRepository _systemUsersRepository;
        private AutoOrder autoOrder;

        #endregion

        #region Private Methods

        private void approveOrder()
        {
            if (!autoOrder.AutoOrderRequestApproved)
            {
                CalculatePrice(autoOrder);

                autoOrder.AutoOrderRequestApproved = true;
                autoOrder.Sys_SystemUser_ID = _systemUsersRepository.GetSystemUserByName(User.UserName).SystemUser_ID;
                _repository.UpdateAutoOrder(autoOrder);
                
                SendEmail(string.Format("Your order approved, and it has number {0}", autoOrder.AutoOrder_ID), autoOrder.Client.ClientEmail);
            }

            _regionManager.Regions["ApplicationContentRegion"].Remove(this.View);
            _eventAggregator.GetEvent<ViewClosedEvent>().Publish(this.View.GetType());
        }

        private void cancel()
        {
            _regionManager.Regions["ApplicationContentRegion"].Remove(this.View);
            _eventAggregator.GetEvent<ViewClosedEvent>().Publish(this.View.GetType());
        }

        private void SendEmail(string message, string email)
        {
            //Send E-mail
        }

        private void CalculatePrice(AutoOrder autoOrder)
        {
            decimal paymentValue = 0;

            if (autoOrder.AutoOrderTypeOfPayment.Equals("d"))
            {
                int withdrawalDay = ((DateTime)autoOrder.AutoOrderWithdrawalDT).DayOfYear;
                int returningDay = ((DateTime)autoOrder.AutoOrderReturningDT).DayOfYear;


                paymentValue = (decimal)autoOrder.AutoClass.AutoClassPriceDailyRate * (returningDay - withdrawalDay);
            }
            else if (autoOrder.AutoOrderTypeOfPayment.Equals("m"))
            {
                paymentValue = (decimal)(autoOrder.Auto.AutoClass.AutoClassPriceMileageRate * autoOrder.AutoOrderMileage);
                
            }

            Payment payment = Payment.CreatePayment(0, paymentValue);
            _paymentsRepository.AddPayment(payment);

            autoOrder.Payment_ID = payment.Payment_ID;
            autoOrder.Payment = payment;
            _repository.UpdateAutoOrder(autoOrder);

            OnPropertyChanged("Orders");
        }

        #endregion
    }
}
